package com.mcptt.config.group.sub.sub;

import java.util.List;

import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;

import com.cloudptt.api.product.log.Log;

public class Entry
{
	public static final String Name = "entry";
	private static final String Name_Uri = "uri";
	private static final String Name_DisplayName = "display-name";
	private static final String Name_UserPriority = "user-priority";
	private static final String Namespace_Xml = "xml";
	private static final String Namespace_Rl = "rl";
	private static final String Namespace_Mcpttgi = "mcpttgi";
	private static final String Name_Lang = "lang";
	private String lang = "en-us";

	private String uri = "";
	private String displayName = "";
	private String userPriority = "";

	public void parse(Element elem) throws JDOMException
	{
		if(elem == null)
		{
			return;
		}
		uri = elem.getAttributeValue(Name_Uri);
		List<Element> list = elem.getChildren();//��ø��ڵ���ӽڵ�  
		for(Element subElem:list) 
		{
			String name = subElem.getName();
			Log.debug(this.getClass(), "name = " + name);
			if(name == null
					|| name.length() <= 0)
			{
				return;
			}
			if(name.equals(Name_UserPriority))
			{
				userPriority = subElem.getText();
			}
			else if(name.equals(Name_DisplayName))
			{
				Namespace namespace = subElem.getNamespace(Namespace_Xml);
				lang = subElem.getAttributeValue(Name_Lang,namespace);
				displayName = subElem.getText();
			}
		}  
	}
	
	public String getUri()
	{
	    return uri;
	}

	public String getDisplayName()
	{
	    return displayName;
	}

	public String getUserPriority()
	{
	    return userPriority;
	}
	
	public String toString()
	{
		StringBuffer strBuf = new StringBuffer("\r\n");
		if(uri != null
				&& uri.length() > 0)
		{
			strBuf.append("<").append(Name).append(" ").append(Name_Uri).append("=\"").append(uri).append("\">").append("\r\n");
		}
		else
		{
			strBuf.append("<").append(Name).append(">").append("\r\n");
		}

		if(displayName != null
				&& displayName.length() > 0)
		{
			strBuf.append("    ").append("<").append(Namespace_Rl).append(":").append(Name_DisplayName).append(" ").append(Namespace_Xml).append(":").append(Name_Lang).append("=\"").append(lang).append("\">");
			strBuf.append(displayName);
			strBuf.append("</").append(Namespace_Rl).append(":").append(Name_DisplayName).append(">").append("\r\n");
		}
		
		strBuf.append("    ").append("<").append(Namespace_Mcpttgi).append(":").append(Name_UserPriority).append(">");
		strBuf.append(userPriority);
		strBuf.append("</").append(Namespace_Mcpttgi).append(":").append(Name_UserPriority).append(">").append("\r\n");

		strBuf.append("</").append(Name).append(">");

		return strBuf.toString();
	}
}
